var optDictId = null, subTable = null, dicts = [] ;
// 配置
var id = {
    table: 'sys_dict_sub_table',
};

var sys_dict = {
    table: {
        id: id.table,
        url: api.system.dict + 'ByParent',
        cols:[
            {field: 'name', title: '全称'},
            {field: 'code', title: '编码'},
            {field: 'shortName', title: '简称'},
            {field: 'py', title: '拼音简写'},
            {field: 'orderNumber', title: '排序'},
        ]
    }
}
initialize({
    module:['dtree'],
    title: '数据字典管理',
    init: function () {
        subTable = soda.table.render(sys_dict.table);
        sys_dict_view.tree();
    }
})

var sys_dict_view = {
    tree: function(){
        sys_dict_data.dict.get(function(d){
            dicts = d.list;
            dtree.render({
                elem: '#dict-tree',
                data: d.tree,
                toolbar:true,
                record:true,
                initLevel: 3,
                toolbarScroll:"#dict-tree-toolbar",
                dot:false,
                skin: "layui",  // layui主题风格
                toolbarBtn:[
                    [
                        {"label":"编码", "name":"code", "type":"text"},
                        {"label":"简称", "name":"shortName", "type":"text"},
                        {"label":"排序", "name":"orderNumber", "type":"text", "value": "0", "placeholder": "数字越大越靠前面"},
                        {"label":"备注", "name":"remark", "type":"textarea"},
                    ],
                    [
                        {"label":"编码", "name":"code", "type":"text"},
                        {"label":"简称", "name":"shortName", "type":"text"},
                        {"label":"排序", "name":"orderNumber", "type":"text", "value": "0", "placeholder": "数字越大越靠前面"},
                        {"label":"备注", "name":"remark", "type":"textarea"},
                    ]
                ],
                toolbarFun: {
                    addTreeNode: function(treeNode, $div){
                        sys_dict_data.dict.create(treeNode, function(){
                            sys_dict_view.tree();
                            sys_dict_data.sub();
                            render.init();
                        });
                    },
                    editTreeLoad: function(treeNode){
                        let node = soda.json.get(dicts, treeNode.nodeId);
                        $('input[name="orderNumber"]').val(node.orderNumber);
                        $('input[name="code"]').val(node.code);
                        $('input[name="shortName"]').val(node.shortName);
                        $('textarea[name="remark"]').val(node.remark);
                    },
                    editTreeNode: function(treeNode, $div){
                        sys_dict_data.dict.update(treeNode, function(){
                            sys_dict_view.tree();
                            sys_dict_data.sub();
                            render.init();
                        });
                    },
                    delTreeNode: function(treeNode, $div){
                        sys_dict_data.dict.delete(treeNode, function(){
                            sys_dict_view.tree();
                            sys_dict_data.sub();
                            render.init();
                        });
                    }
                }
            });

            $(document).on('blur', 'input[name="addNodeName"]', function () {
                let val = $(this).val();
                $('input[name="shortName"]').val(val);
            })
            render.init();

        });
    }
}

var sys_dict_data = {
    dict: {
        get: function (_callback) {
            let param = {
                url: api.system.dict,
                tips: 0,
                callback: _callback
            }
            return soda.http.get(param);
        },
        create: function (_treeNode, _callback) {
            _treeNode.orderNumber = soda.convert.s2n(_treeNode.orderNumber);
            _treeNode.name = _treeNode.context;
            let param = {
                url: api.system.dict,
                data: _treeNode,
                callback: _callback
            }
            return soda.http.post(param);
        },
        update: function(_treeNode, _callback){
            let id = _treeNode.nodeId;
            _treeNode.orderNumber = soda.convert.s2n(_treeNode.orderNumber);
            _treeNode.name = _treeNode.context;
            let param = {
                url: api.system.dict + '/' + id,
                data: _treeNode,
                callback: _callback
            }
            return soda.http.put(param);
        },
        delete: function(_treeNode, _callback){
            let id = _treeNode.nodeId;
            let param = {
                url: api.system.dict + '/' + id,
                callback: _callback
            }
            return soda.http.delete(param);
        }
    },
    sub: function () {
        subTable.reload({
            where: {
                page: 1,
                parentId: optDictId
            }
        })
    }
}

var render = {
    init: function () {
        $('.dtree-layui-item').each(function () {
            let obj = $(this).attr('data-id');
            if(obj == optDictId){
                $(this).addClass('dtree-layui-item-this')
            }
        })
        dtree.on("node('dict-tree')" ,function(obj){
            optDictId = obj.param.nodeId;
            sys_dict_data.sub();
        });
    }
}

